package lumis.service.pagepersonalization.doui.postloadprocessor;

import lumis.doui.source.ISourceData;
import lumis.doui.source.Source;
import lumis.doui.source.TabularData;
import lumis.doui.source.TabularSource;
import lumis.doui.source.postloadprocessor.IPostLoadProcessor;
import lumis.portal.PortalException;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.manager.ManagerFactory;
import lumis.portal.serviceinterfaceinstance.ServiceInterfaceInstanceConfig;
import lumis.util.ITransaction;

import org.w3c.dom.Node;

/**
 * Post load processor responsible for fill extra information about widget.
 * 
 * @version $Revision: 13092 $ $Date: 2011-05-28 18:19:06 -0300 (Sat, 28 May 2011) $
 * @since 6.0.0
 */
public class EditWidgetPostLoadProcessor implements IPostLoadProcessor
{
	@SuppressWarnings("unchecked")
	public void processSource(SessionConfig sessionConfig, Source source, Node parametersNode, ITransaction transaction) throws PortalException
	{
		if (!(source instanceof TabularSource))
			return;

		TabularData data = ((TabularSource<?>) source).getData();
		
		for (ISourceData row : data.getRows())
		{
			String interfaceInstanceId = row.get("interfaceInstanceId", String.class);
			ServiceInterfaceInstanceConfig serviceInterfaceInstanceConfig = ManagerFactory.getServiceInterfaceInstanceManager().get(sessionConfig, interfaceInstanceId, transaction);
			String serviceInstanceId = serviceInterfaceInstanceConfig.getServiceInstanceId();
			String serviceId = ManagerFactory.getServiceInstanceManager().get(serviceInstanceId, transaction).getServiceId();
			String serviceInterfaceId = serviceInterfaceInstanceConfig.getServiceInterfaceId();
			row.put("serviceId", serviceId);
			row.put("serviceInstanceId", serviceInstanceId);
			row.put("interfaceId", serviceInterfaceId);
		}
	}
}
